package service;

import common.Pagination;
import common.StringUtils;
import dao.CroCustomerDao;
import entity.CroCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.CroCustomerVo;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/7 0007.
 */
@Service
public class CroCustomerService {
    @Autowired
    private CroCustomerDao croCustomerDao;

    /*
        根据一个id查找顾客
    */
    @Transactional(readOnly = true)
    public CroCustomer FindCustomerById(String id){
        CroCustomer croCustomer= croCustomerDao.getById(id);
        return croCustomer;
    }

    /**
     * 根据用户名查找顾客
     * @param custemerName 顾客名
     * @return
     */
    @Transactional(readOnly = true)
    public CroCustomer FindCustomerByName(String custemerName){
        CroCustomer croCustomer= croCustomerDao.findByProperty("username",custemerName).get(0);
        return croCustomer;
    }

    /*
    * 分页查找出全部用户
    * */
    @Transactional(readOnly = true)
    public Pagination<CroCustomer> findAllUser(int index,int size){
        String hql="from CroCustomer where del_flag='0'";
        Pagination<CroCustomer> croUserList=croCustomerDao.findPagination(hql,index,size);
        return croUserList;
    }

    /*
    * 根据属性来查找用户
    * */
    @Transactional(readOnly = true)
    public Pagination<CroCustomer> FindUserByProperty(CroCustomerVo croCustomerVo,int index,int size){
        String hql="from CroCustomer where del_flag='0' ";
        StringBuffer sb=new StringBuffer();
        sb.append(hql);
        if(StringUtils.isNotBlank(croCustomerVo.getUsername())){
            sb.append(" and username like '%"+croCustomerVo.getUsername()+"%'");
        }
        if(StringUtils.isNotBlank(croCustomerVo.getType())){
            sb.append(" and type='"+croCustomerVo.getType()+"'");
        }
        if(StringUtils.isNotBlank(croCustomerVo.getStatus())){
            sb.append(" and freeze_flag='"+croCustomerVo.getStatus()+"'");
        }
        if (StringUtils.isNotBlank(croCustomerVo.getPhone())){
            sb.append(" and phone like '%"+croCustomerVo.getPhone()+"%'");
        }
        if(StringUtils.isNotBlank(croCustomerVo.getEmail())){
            sb.append(" and email like '%"+croCustomerVo.getEmail()+"%'");
        }
        if (StringUtils.isNotBlank(croCustomerVo.getStartdate()) && StringUtils.isNotBlank(croCustomerVo.getEnddate())){
            sb.append(" and DATE_FORMAT(signdate,'%Y-%m-%d') between '"+croCustomerVo.getStartdate()+"' and '"+croCustomerVo.getEnddate()+"'");
        }else if (StringUtils.isNotBlank(croCustomerVo.getStartdate()) && !StringUtils.isNotBlank(croCustomerVo.getEnddate())){
            sb.append(" and DATE_FORMAT(signdate,'%Y-%m-%d') >='"+croCustomerVo.getStartdate()+"'");
        }else if(!StringUtils.isNotBlank(croCustomerVo.getStartdate()) && StringUtils.isNotBlank(croCustomerVo.getEnddate())){
            sb.append(" and DATE_FORMAT(signdate,'%Y-%m-%d') <='"+croCustomerVo.getEnddate()+"'");
        }
        Pagination<CroCustomer> croCustomers=croCustomerDao.findPagination(sb.toString(),index,size);
        return croCustomers;
    }

    /*
    * 根据顾客id删除顾客
    * */
    @Transactional(readOnly = false)
    public boolean deleteCus(String id,String del_flag){
        try {
            CroCustomer croCustomer=croCustomerDao.getById(id);
            croCustomer.setDel_flag(del_flag);
            croCustomerDao.update(croCustomer);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 根据顾客id冻结顾客和解冻顾客
    * */
    @Transactional(readOnly = false)
    public boolean freezeCus(String id,String freeze_flag){
        try {
            CroCustomer croCustomer=croCustomerDao.getById(id);
            croCustomer.setFreeze_flag(freeze_flag);
            croCustomerDao.update(croCustomer);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 前台登录：根据邮箱或者手机号进行登录(验证账号是否正确)
    * */
    @Transactional(readOnly = true)
    public CroCustomer FindCusByEmailOrPhone(String username,String type){
        Map<String,Object> map=new HashMap<>();
        if(type.equals("1")){
            map.put("email",username);
        }else{
            map.put("phone",username);
        }
        map.put("del_flag","0");
        map.put("freeze_flag","0");
        try{
            CroCustomer croCustomer=croCustomerDao.findByProperty(map).get(0);
            return croCustomer;
        }catch (IndexOutOfBoundsException e){
            return null;
        }
    }

    /*
    * 插入顾客信息
    * */
    @Transactional(readOnly = false)
    public boolean insertCustomer(CroCustomer croCustomer){
        try {
            croCustomerDao.save(croCustomer);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 更新顾客信息
    * */
    @Transactional(readOnly = false)
    public boolean updateCustomer(CroCustomer croCustomer){
        try {
            croCustomerDao.update(croCustomer);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
